// tts/index.js - 胡萝卜TTS模块
export class TTSModule {
    constructor() {
        this.providers = new Map();
        this.currentProvider = null;
        this.initProviders();
    }

    initProviders() {
        // 动态导入providers
        this.registerProvider('siliconflow', () => import('./siliconflow.js'));
        this.registerProvider('elevenlabs', () => import('./elevenlabs.js'));
        // 未来可以添加更多provider
    }

    registerProvider(name, loader) {
        this.providers.set(name, {
            loader,
            instance: null
        });
    }

    async setProvider(name) {
        if (!name) {
            throw new Error('No TTS provider selected');
        }
        
        if (!this.providers.has(name)) {
            throw new Error(`Unknown TTS provider: ${name}`);
        }

        const provider = this.providers.get(name);
        
        // 延迟加载provider
        if (!provider.instance) {
            const module = await provider.loader();
            const ProviderClass = module.default || module[Object.keys(module)[0]];
            provider.instance = new ProviderClass();
        }

        this.currentProvider = provider.instance;
        return this.currentProvider;
    }

    async synthesize(text, settings) {
        if (!this.currentProvider) {
            throw new Error('No TTS provider selected');
        }
        return await this.currentProvider.synthesize(text, settings);
    }

    async getModels(settings) {
        if (!this.currentProvider) {
            throw new Error('No TTS provider selected');
        }
        return await this.currentProvider.getModels(settings);
    }

    async getVoices(settings) {
        if (!this.currentProvider) {
            throw new Error('No TTS provider selected');
        }
        return await this.currentProvider.getVoices(settings);
    }

    async uploadVoice(data, settings) {
        if (!this.currentProvider) {
            throw new Error('No TTS provider selected');
        }
        
        if (typeof this.currentProvider.uploadVoice !== 'function') {
            throw new Error('此TTS服务不支持上传自定义音色');
        }
        
        return await this.currentProvider.uploadVoice(data, settings);
    }

    async deleteVoice(voiceId, settings) {
        if (!this.currentProvider) {
            throw new Error('No TTS provider selected');
        }
        
        if (typeof this.currentProvider.deleteVoice !== 'function') {
            throw new Error('此TTS服务不支持删除音色');
        }
        
        return await this.currentProvider.deleteVoice(voiceId, settings);
    }
}

// 基础Provider类
export class BaseTTSProvider {
    constructor() {
        this.name = 'Base Provider';
    }

    async synthesize(text, settings) {
        throw new Error('synthesize method must be implemented');
    }

    async getModels(settings) {
        throw new Error('getModels method must be implemented');
    }

    async getVoices(settings) {
        throw new Error('getVoices method must be implemented');
    }

    // 可选方法
    async uploadVoice(data, settings) {
        throw new Error('uploadVoice not supported by this provider');
    }

    async deleteVoice(voiceId, settings) {
        throw new Error('deleteVoice not supported by this provider');
    }

    // 辅助方法
    validateSettings(settings) {
        if (!settings.key) {
            throw new Error('API Key is required');
        }
    }
}

// 导出单例
export default new TTSModule();